home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 243 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.3 KB

  1. Path: fido.asd.sgi.com!austern
  2. From: jbuck@Synopsys.COM (Joe Buck)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Cleaning auto_ptr copy semantics.
  5. Date: 02 Feb 1996 13:54:53 PST
  6. Organization: Synopsys Inc., Mountain View, CA 94043-4033
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4etq2h$acg@hermes.synopsys.com>
  9. References: <gregorDLrrun.K2x@netcom.com>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: 2 Feb 1996 19:52:49 GMT
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBVAwUBMRKIPUy4NqrwXLNJAQGMNwIAyfg6XuO5fQk9WZqyMbTHn8YEFiHO00MN
  14.     qhypQ/MAcTusjDMq1s2CfdTM4v7KvpZVCpty+yltYNg0r0YxvvVitg==
  15.     =21x0
  16. Originator: austern@isolde.mti.sgi.com
  17.  
  18. gregor@netcom.com (Greg Colvin) writes:
  19. >The smallest change I can see that preserves the semantics of strict 
  20. >ownership is to separate the concepts of "holding a pointer" and "owning 
  21. >an object", so that more than one auto_ptr can hold a pointer to an object,
  22. >but only one auto_ptr can own the object.  
  23.  
  24. Your new proposal is a recipe for dangling pointers.  What about the
  25. following program:
  26.  
  27. main () {
  28.     auto_ptr<int> outer = new int;
  29.     {
  30.     auto_ptr<int> inner = outer; // inner now owns the int
  31.     // end of inner's scope, int is deleted
  32.     }
  33.     *outer = 1;            // crash
  34. }
  35.  
  36. Under the old rules for auto_ptr, after the copy constructor for inner,
  37. outer's internal pointer would be null, and I could test for this
  38. (outer.get() == 0).  Under your proposed change, though, outer contains
  39. a dangling pointer.
  40.  
  41. I don't see a way around reference counts if you want a class that
  42. contains a pointer and can do copies and assignments using the expected
  43. semantics and guarantees to clean up the heap object.  
  44.  
  45. We could still have plain auto_ptr, but with no assignment operator and no
  46. copy constructor, and with an explicit member function for transferring
  47. ownership.  This means that a function cannot return an auto_ptr, but it
  48. could still return one through a reference parameter.
  49. -- 
  50. -- Joe Buck     <jbuck@synopsys.com>    (not speaking for Synopsys, Inc)
  51.  
  52. Work for something because it is good,
  53. not just because it stands a chance to succeed.       -- Vaclav Havel
  54. ---
  55. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  56.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  57.   in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
  58.